﻿/// <reference path="jquery-1.8.3.min.js" />
/// <reference path="jquery-ui.min.js" />
var tabs;
var tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>关闭</span></li>";

$(function () {
    toggle_left();
    // left menu
    accordion_left();
    // fit height
    fit_maintable();
    // init main frame
    init_frame();


    $(window).resize(function () { fit_maintable(); tabs.tabs("refresh"); });
});

/*****the functions*****/

// show hide left pannel
function toggle_left() {
    $("#imgtoggle").click(function () {
        $("#left").toggle();
    });
}

function accordion_left() {
    $("#accordion").accordion({ heightStyle: "content" });
}

// fit the maintable to document size
function fit_maintable() {
    var height = $(window).height();
    var top_height = $("#header").height();
    var bottom_height = $("#footer").height();
    var mainTableHeight = height - top_height - bottom_height;

    var left_height = $("#accordion").height();
    if (left_height < mainTableHeight) {
        $("#main table").height(mainTableHeight);
        $("#main_pannel_tabs").height(mainTableHeight - 10);
    }
}

// initialize main frame
function init_frame() {
    // center tabs
    
    tabs = $("#main_pannel_tabs").tabs({ heightStyle: "fill" });
    $(".open_tag").click(function () {
        var tabId = $(this).attr("tabId");
        var label = $(this).attr("title");
        var url = $(this).attr("href");
        openOrAddTab(tabId, label, url);
        return false;
    });

    // close icon: removing the tab on click
    tabs.delegate("span.ui-icon-close", "click", function () {
        var panelId = $(this).closest("li").remove().attr("aria-controls");
        $("#" + panelId).remove();
        tabs.tabs("refresh");
    });
    tabs.bind("keyup", function (event) {
        if (event.altKey && event.keyCode === $.ui.keyCode.BACKSPACE) {
            var panelId = tabs.find(".ui-tabs-active").remove().attr("aria-controls");
            $("#" + panelId).remove();
            tabs.tabs("refresh");
        }
    });
}


// open or add tab
function openOrAddTab(tabId, label, url) {
    if (!openTab(tabId)) {
        addTab(tabId, label, url);
        openTab(tabId);
    }
}

// actual addTab function: adds new tab using the input from the form above
function addTab(tabId, label, url) {
    var li = $(tabTemplate.replace(/#\{href\}/g, "#" + tabId).replace(/#\{label\}/g, label));
    tabs.find(".ui-tabs-nav").append(li);
    tabs.append("<iframe id='" + tabId + "' src=\"" + url + "\" class=\"work_banch\" frameborder=\"0\"></iframe>");
    tabs.tabs("refresh");
}

// open tab
function openTab(tabId) {
    var index = $("#" + tabId).index();
    if (index >= 0) {
        tabs.tabs("option", "active", index - 1);
        return true;
    }
    else {
        return false;
    }
}

function fitIFrameHeight(ifm) {
    var clientHeight = $(ifm).contents().find("body").height() + 20;
    $(ifm).height(clientHeight);
}

